package com.zhhtech.zhh.api

import com.zhhtech.zhh.BuildConfig
import com.zhhtech.zhh.api.core.ApiHelper
import com.zhhtech.zhh.api.core.ResultBean
import com.zhhtech.zhh.bean.*
import com.zhhtech.zhh.ui.interceptor.CancelConvert
import com.zhhtech.zhh.ui.interceptor.ConvertInterceptor
import kotlinx.coroutines.CoroutineScope
import okhttp3.ResponseBody
import retrofit2.http.*

val API_SERVICE: com.zhhtech.zhh.api.ApiService by lazy {
    ApiHelper.create(com.zhhtech.zhh.api.ApiService::class.java, {
        baseUrl(com.zhhtech.zhh.BuildConfig.BASE_URL)
    }, {
        addInterceptor(ConvertInterceptor())
    })
}
val CoroutineScope.api get() = com.zhhtech.zhh.api.API_SERVICE

interface ApiService {
    /**
     * 发送验证码
     */
    @POST("/api/code/send")
    @FormUrlEncoded
    @CancelConvert
    suspend fun codeSend(
        @Field("phone") phone: String,
    ): ResultBean<Any?>

    /**
     * 注销接口
     */
    @GET("/api/user/logOut")
    suspend fun userLogOut(): ResultBean<UserLogOut?>

    /**
     * 一键登录
     */
    @POST("/api/user/aliClickLogin")
    @FormUrlEncoded
    suspend fun aliClickLogin(
        @Field("accessToken") accessToken: String,
        @Field("imei") imei: String,
        @Field("channel") deviceType: String = com.zhhtech.zhh.App.channel,
    ): ResultBean<TokenBean?>

    /**
     * 验证码登录
     */
    @POST("/api/user/login")
    @FormUrlEncoded
    @CancelConvert
    suspend fun userLogin(
        @Field("phone") phone: String,
        @Field("code") code: String,
        @Field("imei") imei: String,
        @Field("channel") channel: String = com.zhhtech.zhh.App.channel,
    ): ResultBean<TokenBean?>

    /**
     * 获取统一联盟签名秘钥
     */
    @GET("http://qingsonghai.oss-cn-hangzhou.aliyuncs.com/pem/com.zhhtech.zhh.cert.pem")
    suspend fun cert(): ResponseBody

    /**
     * 用户信息
     */
    @GET("/api/user/index")
    suspend fun userInfo(): ResultBean<UserInfoBean?>

    /**
     * 激活
     */
    @GET("/api/index/activate")
    suspend fun activate(): ResultBean<Any?>

    /**
     * 版本检查更新
     */
    @GET("/api/index/version")
    suspend fun version(
        @Query("version") version: String,
        @Query("deviceType") deviceType: String = com.zhhtech.zhh.App.channel,
    ): ResultBean<Version?>

    /**
     * 首页信息（banner和案例）
     */
    @GET("/api/index/home")
    suspend fun home(): ResultBean<HomeBean?>

    /**
     * 配置
     */
    @GET("/api/index/config")
    suspend fun indexConfig(): ResultBean<CaseConfig?>

    /**
     * 创建订单
     */
    @POST("/api/order/create")
    @FormUrlEncoded
    suspend fun orderCreate(
        @Field("loanType") loanType: String,
        @Field("amount") amount: String,
        @Field("username") username: String,
    ): ResultBean<CaseOrderBean?>

    /**
     * 方案进程
     */
    @GET("/api/order/orderProcess")
    suspend fun orderProcess(): ResultBean<OrderProcess?>

    /**
     * 分享
     */
    @GET("/api/user/share")
    suspend fun userShare(): ResultBean<ShareBean?>

    /**
     * 案例列表
     */
    @GET("/api/index/caseList")
    suspend fun caseList(@Query("page") page: Int): ResultBean<PageInfo<HomeCaseBean>?>

    /**
     * 邀请列表
     */
    @GET("/api/user/inviteList")
    suspend fun inviteList(): ResultBean<InviteList?>

    /**
     * 收益列表
     */
    @GET("/api/user/incomeList")
    suspend fun incomeList(): ResultBean<PageInfo<IncomeBean>?>

    /**
     * 提现
     */
    @POST("/api/user/withdraw")
    @FormUrlEncoded
    suspend fun withdraw(@Field("amount") amount: String): ResultBean<Any?>

    /**
     * 设置邀请关系
     */
    @POST("/api/user/setInviter")
    @FormUrlEncoded
    suspend fun setInviter(@Field("inviteCode") inviteCode: String): ResultBean<Any?>

    /**
     * 订单列表
     */
    @POST("/api/order/list")
    @FormUrlEncoded
    suspend fun orderList(@Field("orderType") orderType: Int? = null): ResultBean<PageInfo<CaseOrderBean>?>

    /**
     * 取消订单
     */
    @POST("/api/order/cancel")
    @FormUrlEncoded
    suspend fun orderCancel(@Field("id") id: String): ResultBean<Any?>
}